package com.www.applyword.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.HomeJobAdapter;
import com.www.applyword.adapter.MyJobAdapter;
import com.www.applyword.adapter.MyJobAdapter2;
import com.www.applyword.base.BaseFragment;
import com.www.applyword.bean.HotJobBean;
import com.www.applyword.bean.JobApplyBean;
import com.www.applyword.bean.JobListBean;
import com.www.applyword.bean.OrderBean;
import com.www.applyword.event.EventConstants;
import com.www.applyword.event.MessageEvent;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.ui.home.HomeJobActivity;
import com.www.applyword.ui.home.HotJobDetailsActivity;
import com.www.applyword.utils.ListDataSave2;
import com.www.applyword.utils.SPUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * author : huang
 * date   : 2019/10/22 0022 15:16
 * version: 1.0
 */
public class HaveReleaseFragment extends BaseFragment {
    @BindView(R.id.rl_job)
    RecyclerView rlJob;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refresh;

    ViewStub vsEmpty;

    private MyJobAdapter2 mHotJobAdapter;
    private List<HotJobBean> hotJobBeans = new ArrayList<>();

    private int page = 1;
    private int Rows = 10;
    private String url;
    private HomeJobAdapter homeJobAdapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.launched_fragment;
    }



    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        vsEmpty =  rootView.findViewById(R.id.vs_empty);
        refresh.setOnRefreshListener(refreshLayout -> initDate(true));
        refresh.setOnLoadMoreListener(refreshLayout ->initDate(false));
        rlJob.setLayoutManager(new LinearLayoutManager(getActivity()));
        rlJob.setItemAnimator(new DefaultItemAnimator());
        homeJobAdapter = new HomeJobAdapter(getActivity());
        rlJob.setAdapter(homeJobAdapter);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        initDate(true);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.PAY)) {
                initDate(true);
            }
        }
    }


    private void initDate(boolean isRefresh) {
        List<JobListBean> jobListBeans = ListDataSave2.getInstence(getActivity()).getDataList();
        homeJobAdapter.replaceAll(jobListBeans, true);
        if (isRefresh) {
            refresh.finishRefresh(true);
        }else {
            refresh.finishLoadMoreWithNoMoreData();
        }
        homeJobAdapter.setOnItemClickListener(new CommonRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int i) {
                startActivity(new Intent(getActivity(), HomeJobActivity.class).putExtra("id",homeJobAdapter.getItem(i).getPartJobId())
                        .putExtra("jobListBeans",homeJobAdapter.getItem(i)));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}

